<?php
declare (strict_types=1);

namespace app\validate;

use app\model\Company;
use app\model\DyUser;
use app\model\ExpressCompany;
use app\model\Role;
use app\model\SalesManTeam;
use app\model\User;
use app\model\Wiseman;
use think\facade\Db;
use think\facade\Log;
use think\Validate;

class BaseValidate extends Validate
{

    //验证省
    public function checkProvince($province)
    {
        $provinceExist = Db::name('china_districts')
            ->where(['code' => $province, 'province_code' => 0, 'city_code' => 0])->find();

        if (!$provinceExist) {
            return false;
        }
        return true;
    }

    //验证市
    public function checkCity($value, $rule, $data = [])
    {
        $cityExist = Db::name('china_districts')
            ->where(['code' => $value, 'province_code' => $data['province'], 'city_code' => 0])->find();
        if (!$cityExist) {
            return false;
        }
        return true;
    }

    //验证区
    public function checkDistrict($value, $rule, $data = [])
    {
        $district = Db::name('china_districts')
            ->where(['code' => $value, 'province_code' => $data['province'], 'city_code' => $data['city']])->find();
        if (!$district) {
            return false;
        }
        return true;
    }


    public function checkShopIds($value, $rule, $data = [])
    {
//        Log::info($value);
//        Log::info($data);
        return true;
    }


    //检测发布活动设置门槛
    public function checkThreshold($value, $rule, $data)
    {
        if ($data['activity_type'] == 2 && $value == 1) {
            return false;
        }
        //指定商家报名方式不需要设置门槛
        return true;
    }


    public function checkTeamId($teamId)
    {
        if ($teamId == 0) {
            return true;
        }
        $teamExist = SalesManTeam::where(['id' => $teamId])->findOrEmpty();
        if (!$teamExist->isEmpty()) {
            return true;
        }
        return false;
    }

    public function checkPhone($phone)
    {
        $phoneExist = User::where(['phone' => $phone])->findOrEmpty();
        if ($phoneExist->isEmpty()) {
            return true;
        }
        return false;
    }

    public function checkCompany($companyId)
    {
        $companyExist = Company::findOrEmpty($companyId);
        if ($companyExist) {
            return true;
        }
        return false;
    }

    public function checkRole($roleId)
    {
        if ($roleId == 1) {
            return false;
        }
        $roleExist = Role::findOrEmpty($roleId);
        if ($roleExist) {
            return true;
        }
        return false;
    }

    //检测dy_user_id是否合法
    public function checkDyUser($dyUserId)
    {
        $dyUser = Wiseman::where(['id'=>$dyUserId])->field('id')->findOrEmpty();
        if($dyUser->isEmpty()){
            return false;
        }
        return true;
    }

    //检测express_company_id是否合法
    public function checkExpressCompany($expressCompanyId)
    {
        $expressCompany = ExpressCompany::where(['id'=>$expressCompanyId])->field('id')->findOrEmpty();
        if($expressCompany->isEmpty()){
            return false;
        }
        return true;
    }

}
